<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>企业管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="company-app">
    <div class="container">
        <div class="tableBar">
            <el-input
                    v-model="input"
                    placeholder="请输入企业名称"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="init"
                ></i>
            </el-input>
            <div class="tableLab">
                <el-button  type="primary" class="continue" @click="addClass('class')"> + 新增企业 </el-button>
            </div>
        </div>
        <el-table  :data="tableData"  stripe  class="tableBox" >
            <el-table-column  prop="name" label="企业名称"  /></el-table-column>
            <el-table-column  prop="representative" label="企业法人"  /></el-table-column>
            <el-table-column label="公司规模" >
                <template slot-scope="scope">
                    <span v-if="scope.row.companySize == 'micro'">1-99人</span>
                    <span v-if="scope.row.companySize == 'small'">100-199人</span>
                    <span v-if="scope.row.companySize == 'middle'">200-499人</span>
                    <span v-if="scope.row.companySize == 'big'">500-1000人</span>
                    <span v-if="scope.row.companySize == 'large'">1000人以上</span>
                </template>
            </el-table-column>
            <el-table-column  prop="industry" label="所属行业"  /></el-table-column>
            <el-table-column  prop="city" label="所在城市"  /></el-table-column>
            <el-table-column prop="expirationDate"  label="过期时间" >
                <template slot-scope="scope">
                    {{scope.row.expirationDate}}
                </template>
            </el-table-column>
            <el-table-column  label="公司状态">
                <template slot-scope="scope">
                    {{ String(scope.row.state) === '0' ? '已禁用' : '正常' }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="editHandle(scope.row)">修改</el-button>
                    <el-button type="text" size="small" class="delBut non" @click="deleteHandle(scope.row.id)">停用</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                :current-page.sync="page"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="企业名称：">
                <el-input v-model="classData.formData.name" placeholder="请输入企业名称" maxlength="14"/>
            </el-form-item>

            <el-form-item label="企业地址：">
                <el-input v-model="classData.formData.address" placeholder="请输入企业地址" maxlength="14"/>
            </el-form-item>

            <el-form-item label="企业法人：">
                <el-input v-model="classData.formData.representative" placeholder="请输入企业法人" maxlength="14"/>
            </el-form-item>

            <el-form-item label="联系电话：">
                <el-input v-model="classData.formData.phone" type="number" placeholder="请输入联系电话"/>
            </el-form-item>

            <el-form-item label="公司规模：">
                <el-select v-model="classData.formData.companySize" placeholder="请输入公司规模">
                    <el-option
                            v-for="item in companySizeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="所属行业：">
                <el-input v-model="classData.formData.industry" placeholder="请输入所属行业"/>
            </el-form-item>

            <el-form-item label="所在城市：">
                <el-input v-model="classData.formData.city" placeholder="请输入所在城市"/>
            </el-form-item>

            <el-form-item label="账户余额：">
                <el-input v-model="classData.formData.balance" type="number" placeholder="请输入账户余额"/>
            </el-form-item>

            <el-form-item label="营业执照：">
                <el-input v-model="classData.formData.licenseId" placeholder="请输入营业执照"/>
            </el-form-item>

            <el-form-item label="过期时间：">
                <el-date-picker
                        v-model="classData.formData.expirationDate"
                        type="date"
                        format="yyyy-MM-dd"
                        placeholder="请选择到期时间">
                </el-date-picker>
            </el-form-item>

            <el-form-item label="企业简介：">
                <el-input v-model="classData.formData.remark" type="textarea" placeholder="请输入企业简介"/>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="classData.dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="medium" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
</div>
<script src="../../plugins/vue/vue.js"></script>
<script src="../../plugins/element-ui/index.js"></script>
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/company.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#company-app',
        data() {
            return {
                action: '',
                tableData: [],
                type: '',
                classData: {
                    'title': '添加企业',
                    'dialogVisible': false,
                    formData:{},
                },
                companySizeList: [{
                    value: 'micro',
                    label: '1-99人'
                }, {
                    value: 'small',
                    label: '100-199人'
                }, {
                    value: 'middle',
                    label: '200-499人'
                }, {
                    value: 'big',
                    label: '500-1000人'
                }, {
                    value: 'large',
                    label: '1000人以上'
                }],
                page: 1,
                pageSize: 5,
                counts:0,
                input:''
            }
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,//当前页码
                    pageSize: this.pageSize,//每页大小
                    name: this.input ? this.input : undefined//企业名称
                };
                await getCompanyPage(params).then(res => {
                    //console.log(res.data.records)
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records;
                        this.counts = res.data.total;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加
            addClass(st) {
                this.classData.title = '新增企业';
                this.action = 'add';
                this.classData.dialogVisible = true
            },
            editHandle(dat) {
                //赋值
                this.classData.title = '修改企业';
                this.action = 'edit';
                this.classData.formData = dat;
                this.classData.dialogVisible = true;//打开修改框
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteCompany(id).then(res => {
                        if (res.code === 1) {
                            this.$message.success('删除成功！');
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                const classData = this.classData.formData;
                const valid = (classData.name === 0 || classData.name) ;
                if (this.action === 'add') {
                    if (valid) {
                            addCompany(classData).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('添加成功！')
                                    if (!st) {
                                        this.classData.dialogVisible = false
                                    } else {
                                        this.classData = {};
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                    } else {
                        this.$message.error('请输入名称')
                    }
                } else if (valid) {
                    editCompany(classData).then(res => {
                        if (res.code === 1) {
                                this.$message.success('修改成功！');
                                this.classData.dialogVisible = false;
                                this.handleQuery()
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })
                } else {
                    this.$message.error('请输入名称')
                }
            },
            // 全部操作
            handleSelectionChange(val) {
                let checkArr = [];
                val.forEach((n) => {
                    checkArr.push(n.id)
                });
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val;
                this.init()
            }
        },
        created() {
            this.init()
        }
    })
</script>
</body>
</html>